home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / bin / ucf < prev    next >
Text File  |  2009-08-25  |  38KB  |  1,055 lines

  1. #!/bin/bash
  2. #                               -*- Mode: Sh -*-
  3. # updateConfFile.sh ---
  4. # Author           : Manoj Srivastava ( srivasta@glaurung.green-gryphon.com )
  5. # Created On       : Fri Feb  1 03:41:47 2002
  6. # Created On Node  : glaurung.green-gryphon.com
  7. # Last Modified By : Manoj Srivastava
  8. # Last Modified On : Tue Jun  6 09:48:22 2006
  9. # Last Machine Used: glaurung.internal.golden-gryphon.com
  10. # Update Count     : 186
  11. # Status           : Unknown, Use with caution!
  12. # HISTORY          :
  13. # Description      :
  14. #
  15. # This script attempts to provide conffile like handling for files not
  16. # shipped in a Debian package, but handled by the postinst. Using this
  17. # script, one may ship a bunch of default cofiguration files somewhere
  18. # in /usr (/usr/share/<pkg> is a good location), and maintain files in
  19. # /etc.
  20. #
  21. # The motivation for this script was to provide conffile like handling
  22. # for start files for emacs lisp packages (for example,
  23. # /etc/emacs21/site-stard.d/50psgml-init.el) These start files are not
  24. # shipped with the package, instead, they are installed during the
  25. # post installation configuration phase by the script
  26. # /usr/lib/emacsen-common/emacs-package-install $package_name.
  27. #
  28. # This script is meant to be invoked by the packages install script at
  29. # /usr/lib/emacsen-common/packages/install/$package_name for each
  30. # flavour of installed emacsen by calling it with the proper values of
  31. # new file (/usr/share/emacs/site-lisp/<pkg>/<pkg>-init.el), and dest file
  32. # (/etc/emacs21/site-stard.d/50<pkg>-init.el)), and it should do the rest.
  33. #
  34.  
  35. # make sure we exit on error
  36. set -e
  37.  
  38. # set the version and revision
  39. progname="`basename \"$0\"`"
  40. pversion='$Revision: 1.26 $'
  41.  
  42. ######################################################################
  43. ########                                                     #########
  44. ########              Utility functions                      #########
  45. ########                                                     #########
  46. ######################################################################
  47. setq() {
  48.     # Variable Value Doc_string
  49.     if [ "x$2" = "x" ]; then
  50.     echo >&2 "$progname: Unable to determine $3"
  51.     exit 1;
  52.     else
  53.     if [ "x$VERBOSE" != "x" ]; then
  54.         echo >&2 "$progname: $3 is $2";
  55.     fi
  56.     eval "$1=\"\$2\"";
  57.     fi
  58. }
  59.  
  60. # Use debconf to show the differences
  61. show_diff() {
  62.     if [ -z "$1" ]; then
  63.     DIFF="There are no non-white space differences in the files."
  64.     else
  65.         if  [ 99999 -lt $(echo $1 | wc -c | awk '{print $1; }') ]; then
  66.             DIFF="The differences between the files are too large to display."
  67.         else
  68.             DIFF="$1"
  69.         fi
  70.     fi
  71.     if [ "$DEBCONF_OK" = "YES" ] && [ "$DEBIAN_HAS_FRONTEND" ]; then
  72.     templ=ucf/show_diff
  73.     db_capb escape
  74.     db_subst $templ DIFF "$(printf %s "$DIFF" | debconf-escape -e)"
  75.     db_fset $templ seen false
  76.     db_input critical $templ || true
  77.     db_go || true
  78.     db_get $templ
  79.     # may contain sensitive information, so clear
  80.     # immediatly after use so it is never written
  81.     # to disk
  82.     db_subst $templ DIFF ""
  83.     db_reset $templ 
  84.     db_capb
  85.     else
  86.     echo "$DIFF" | sensible-pager
  87.     fi
  88. }
  89.  
  90. withecho () {
  91.         echo " $@" >&2
  92.         "$@"
  93. }
  94.  
  95. usageversion () {
  96.         cat >&2 <<END
  97. Debian GNU/Linux $progname $pversion.
  98.            Copyright (C) 2002-2005 Manoj Srivastava.
  99. This is free software; see the GNU General Public Licence for copying
  100. conditions.  There is NO warranty.
  101.  
  102. Usage: $progname  [options] new_file  destination
  103. Options:
  104.      -h,     --help          print this message
  105.      -s foo, --src-dir  foo  Set the src dir (historical md5sums live here)
  106.              --sum-file bar  Force the historical md5sums to be read from
  107.                              this file.  Overrides any setting of --src-dir.
  108.      -d [n], --debug    [n]  Set the Debug level to N
  109.      -n,     --no-action     Dry run. No action is actually taken.
  110.      -v,     --verbose       Make the script verbose
  111.              --three-way     Register this file in the cache, and turn on the
  112.                              diff3 option allowing the merging of maintainer
  113.                              changes into a (potentially modified) local 
  114.                              configuration file. )
  115.              --state-dir bar Set the state directory to bar instead of the
  116.                              default '/var/lib/ucf'. Used mostly for testing.
  117.              --debconf-ok    Indicate that it is ok for uct to use an already
  118.                              running debconf instance for prompting.
  119.              --debconf-template bar
  120.                              Specify an alternate, caller-provided debconf
  121.                              template to use for prompting.
  122. Usage: $progname  -p  destination
  123.      -p,     --purge         Remove any reference to destination from records
  124.  
  125. By default, the directory the new_file lives in is assumed to be the src-dir,
  126. which is where we look for any historical md5sums.
  127.  
  128. END
  129.     
  130. }
  131.  
  132. ######################################################################
  133. ########                                                     #########
  134. ########        file and hash save/restore functions         #########
  135. ########                                                     #########
  136. ######################################################################
  137. purge_md5sum () {
  138.     for i in $(/usr/bin/seq 6 -1 0); do
  139.     if [ -e "${statedir}/hashfile.${i}" ]; then
  140.         if [ "X$docmd" = "XYES" ]; then
  141.         cp -pf "${statedir}/hashfile.${i}" \
  142.             "${statedir}/hashfile.$(($i+1))"
  143.         else 
  144.         echo cp -pf "${statedir}/hashfile.${i}" \
  145.                           "${statedir}/hashfile.$(($i+1))"
  146.         fi 
  147.     fi 
  148.     done
  149.     if [ -e "$statedir/hashfile" ]; then
  150.     if [ "X$docmd" = "XYES" ]; then
  151.         cp -pf "$statedir/hashfile"  "$statedir/hashfile.0" 
  152.     else
  153.         echo cp -pf "$statedir/hashfile"  "$statedir/hashfile.0" 
  154.     fi
  155.     if [ "X$docmd" = "XYES" ]; then
  156.         set +e
  157.         if [ "X$VERBOSE" != "X" ]; then
  158.         echo "egrep -v [[:space:]]${safe_dest_file}$ $statedir/hashfile"
  159.         egrep -v "[[:space:]]${safe_dest_file}$"  "$statedir/hashfile" \
  160.             || true;
  161.         fi
  162.         #echo "egrep -v [[:space:]]${safe_dest_file}$ $statedir/hashfile"
  163.         egrep -v "[[:space:]]${safe_dest_file}$" "$statedir/hashfile" > \
  164.         "$statedir/hashfile.tmp" || true; 
  165.         if [ "X$docmd" = "XYES" ]; then
  166.         mv -f "$statedir/hashfile.tmp"  "$statedir/hashfile" 
  167.         else
  168.         echo mv -f "$statedir/hashfile.tmp"  "$statedir/hashfile"
  169.         fi
  170.         set -e
  171.     fi
  172.     fi
  173.     test -n "$VERBOSE" && echo "The cache file is $cached_file"
  174.     if [ ! -z "$cached_file" -a -f "$statedir/cache/$cached_file" ]; then
  175.     $action rm -f "$statedir/cache/$cached_file"
  176.     fi
  177. }
  178.  
  179. replace_md5sum () {
  180.     for i in $(/usr/bin/seq 6 -1 0); do
  181.     if [ -e "${statedir}/hashfile.${i}" ]; then
  182.         if [ "X$docmd" = "XYES" ]; then
  183.         cp -pf "${statedir}/hashfile.${i}" \
  184.             "${statedir}/hashfile.$(($i+1))"
  185.         else
  186.         echo cp -pf "${statedir}/hashfile.${i}" \
  187.             "${statedir}/hashfile.$(($i+1))"
  188.         fi
  189.     fi
  190.     done
  191.     if [ -e "$statedir/hashfile" ]; then
  192.     if [ "X$docmd" = "XYES" ]; then
  193.         cp -pf "$statedir/hashfile"  "$statedir/hashfile.0" 
  194.     else
  195.         echo cp -pf "$statedir/hashfile"  "$statedir/hashfile.0" 
  196.     fi
  197.     if [ "X$docmd" = "XYES" ]; then
  198.         set +e
  199.         if [ "X$VERBOSE" != "X" ]; then
  200.         echo "(egrep -v \"[[:space:]]${safe_dest_file}$\" \"$statedir/hashfile\";"
  201.         egrep -v "[[:space:]]${safe_dest_file}$"  "$statedir/hashfile" || true;
  202.          md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|" ;
  203.         fi
  204.         egrep -v "[[:space:]]${safe_dest_file}$" "$statedir/hashfile" > \
  205.         "$statedir/hashfile.tmp" || true; 
  206.         md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|" >> \
  207.         "$statedir/hashfile.tmp"; 
  208.         mv -f "$statedir/hashfile.tmp"  "$statedir/hashfile" 
  209.         set -e
  210.     else
  211.         echo "(egrep -v \"[[:space:]]${safe_dest_file}$\" \"$statedir/hashfile\""
  212.         echo " md5sum \"$orig_new_file\" | sed \"s|$orig_new_file|$dest_file|\"; " 
  213.         echo ") | sort > \"$statedir/hashfile\""
  214.     fi
  215.     else
  216.     if [ "X$docmd" = "XYES" ]; then
  217.         md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|"  > \
  218.         "$statedir/hashfile" 
  219.     else
  220.         echo " md5sum \"$orig_new_file\" | sed \"s|$orig_new_file|$dest_file|\" >" \
  221.         "\"$statedir/hashfile\""
  222.     fi
  223.     fi
  224.     file_size=$(stat -c '%s' "$orig_new_file")
  225.     if [ "X$THREEWAY" != "X" ] || [ $file_size -lt 25600 ]; then
  226.     $action cp -pf "$orig_new_file" "$statedir/cache/$cached_file"
  227.     fi
  228.     # cp -pf "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  229. }
  230.  
  231. replace_conf_file () {
  232.     # do not mangle $dest_file since it's the one registered in the hashfile
  233.     # or we have been ask to register
  234.     real_file="$dest_file"
  235.     if [ -L "$dest_file" ]; then
  236.     real_file="$(readlink -nf $dest_file || :)"
  237.     if [ "x$real_file" = "x" ]; then
  238.         echo >&2 "$dest_file is a broken symlink!"
  239.         $action rm -f "$dest_file";
  240.         real_file="$dest_file"
  241.     fi
  242.     fi
  243.     if [ -e "$real_file" ]; then
  244.     if [ -z "$RETAIN_OLD" ]; then
  245.         #echo "Saving  ${real_file}.${OLD_SUFFIX},  in case."
  246.         if [ "x$VERBOSE" != "x" ]; then
  247.         echo >&2 "Not saving ${real_file}, since it was unmodified"
  248.         fi
  249.     else
  250.         $action cp -pf "${real_file}" "${real_file}.${OLD_SUFFIX}"
  251.     fi
  252.     fi
  253.     $action cp -pf "$new_file" "${real_file}"
  254.     replace_md5sum;
  255. }
  256.  
  257. # Escape single quotes in the arguments passed in
  258. quote_single() {
  259.     echo "$1" | sed -e "s,','\\\\'',g"
  260. }
  261.  
  262.  
  263.  
  264. ######################################################################
  265. ########                                                     #########
  266. ########              Command line args                      #########
  267. ########                                                     #########
  268. ######################################################################
  269. #
  270. # Long term variables#
  271. #
  272. docmd='YES'
  273. action='withecho'
  274. action=
  275. DEBUG=0
  276. VERBOSE=''
  277. statedir='/var/lib/ucf';
  278. THREEWAY=
  279.  
  280. DIST_SUFFIX="ucf-dist"
  281. NEW_SUFFIX="ucf-new"
  282. OLD_SUFFIX="ucf-old"
  283.  
  284. # save up the cmdline with proper quoting/escaping
  285. for arg in "$@"; do
  286.     saved="${saved:+$saved }'$(quote_single "$arg")'"
  287. done
  288.  
  289.  
  290. # Note that we use `"$@"' to let each command-line parameter expand to a
  291. # separate word. The quotes around `$@' are essential!
  292. # We need TEMP as the `eval set --' would nuke the return value of getopt.
  293. TEMP=`getopt -a -o hs:d::D::nv -n "$progname" \
  294.       --long help,src-dir:,sum-file:,dest-dir:,debug::,DEBUG::,no-action,purge,verbose,three-way,debconf-ok,debconf-template:,state-dir: \
  295.              -- "$@"`
  296.  
  297. if [ $? != 0 ] ; then
  298.     echo "Error handling options.Terminating..." >&2 ;
  299.     exit 1 ;
  300. fi
  301.  
  302. # Note the quotes around `$TEMP': they are essential!
  303. eval set -- "$TEMP"
  304.  
  305. while true ; do
  306.     case "$1" in
  307.     -h|--help) usageversion;                        exit 0 ;;
  308.     -n|--no-action) action='echo'; docmd='NO';      shift  ;;
  309.     -v|--verbose) VERBOSE=1;                        shift  ;;
  310.     -s|--src-dir)
  311.         opt_source_dir="$2";                       shift 2 ;;
  312.     --sum-file)
  313.         opt_old_mdsum_file="$2";          shift 2 ;;
  314.     --state-dir)
  315.         opt_state_dir="$2";                        shift 2 ;;
  316.     --debconf-template)
  317.         override_template="$2";                    shift 2 ;;
  318.     -D|-d|--debug|--DEBUG)
  319.             # d has an optional argument. As we are in quoted mode,
  320.             # an empty parameter will be generated if its optional
  321.             # argument is not found.
  322.         case "$2" in
  323.         "") setq DEBUG 1    "The Debug value"; shift 2 ;;
  324.         *)  setq DEBUG "$2" "The Debug value"; shift 2 ;;
  325.         esac ;;
  326.         -p|--purge) PURGE=YES;                         shift   ;;
  327.        --three-way) THREEWAY=YES;                       shift   ;;
  328.        --debconf-ok) DEBCONF_OK=YES;                    shift   ;;
  329.     --)  shift ;                                   break   ;;
  330.     *) echo >&2 "Internal error!" ; exit 1 ;;
  331.     esac
  332. done
  333.  
  334.  
  335. ######################################################################
  336. ########                                                     #########
  337. ########              Sanity checking                        #########
  338. ########                                                     #########
  339. ######################################################################
  340. # Need to run as root, or else the 
  341. if test $(id -u) != 0; then
  342.     if [ "$docmd" = "YES" ]; then
  343.         echo "$progname: Need to be run as root." >&2
  344.         echo "$progname: Setting up no action mode." >&2
  345.         action='echo'; docmd='NO'; 
  346.     fi
  347. fi
  348.  
  349. if [ "X$PURGE" = "XYES" ]; then
  350.     if [ $# != 1 ]; then
  351.     echo >&2 "*** ERROR: Need exactly one argument when purging, got $#";
  352.     echo >&2 ""
  353.     usageversion;
  354.     exit 0 ;        
  355.     fi
  356.     temp_dest_file=$1;
  357.     setq dest_file "$(readlink -q -m $temp_dest_file)" "The Destination file";
  358. else
  359.     if [ $# != 2 ]; then
  360.     echo >&2 "*** ERROR: Need exactly two arguments, got $#";
  361.     echo >&2 ""
  362.     usageversion;
  363.     exit 0 ;
  364.     fi
  365.     temp_new_file=$1;
  366.     temp_dest_file=$2;
  367.  
  368.     if [ ! -e "$temp_new_file" ]; then
  369.     echo >&2 "Error: The new file ${temp_new_file} does not exist!";
  370.     exit 1;
  371.     fi
  372.     setq new_file  "$(readlink -q -m $temp_new_file)"  "The new file";
  373.     setq dest_file "$(readlink -q -m $temp_dest_file)" "The Destination file";
  374. fi
  375.  
  376.  
  377. safe_dest_file=$(echo $dest_file | perl -nle 'print "\Q$_\E\n"')
  378.  
  379.  
  380.  
  381. ######################################################################
  382. ########                                                     #########
  383. ########              Set Default Values                     #########
  384. ########                                                     #########
  385. ######################################################################
  386. # Load site defaults and over rides.
  387. if [ -f /etc/ucf.conf ]; then
  388.     . /etc/ucf.conf
  389. fi
  390.  
  391. # Command line, env variable, config file, or default
  392. if [ ! "x$opt_source_dir" = "x" ]; then
  393.     setq source_dir "$opt_source_dir" "The Source directory"
  394. elif [ ! "x$UCF_SOURCE_DIR" = "x" ]; then
  395.     setq source_dir "$UCF_SOURCE_DIR" "The Source directory"
  396. elif [ ! "x$conf_source_dir" = "x" ]; then
  397.     setq source_dir "$conf_source_dir" "The Source directory"
  398. else
  399.     if [ "X$new_file" != "X" ]; then
  400.     setq source_dir $(dirname "$new_file") "The Source directory"
  401.     else
  402.     setq source_dir /tmp "The Source directory"
  403.     fi
  404.     
  405. fi
  406.  
  407. if [ "X$PAGER" != "X" ] && which $PAGER >/dev/null 2>&1 ; then
  408.     my_pager=$(which $PAGER);
  409. elif [ -s /usr/bin/pager ] && 
  410.      [ "X$(readlink -e /usr/bin/pager || :)" != "X" ]; then
  411.     my_pager=/usr/bin/pager
  412. elif [ -x /usr/bin/sensible-pager ]; then
  413.     my_pager=/usr/bin/sensible-pager
  414. elif [ -x /bin/more ]; then
  415.     my_pager=/bin/more
  416. else
  417.     my_pager=
  418. fi
  419.  
  420.  
  421.  
  422. if [ "X$my_pager" = "X" ]; then
  423.     STOP=YES
  424. elif [ "X$my_pager" = "X/bin/more" ]; then
  425.     STOP=YES
  426. fi
  427.  
  428. # Command line, env variable, config file, or default
  429. if [ ! "x$opt_state_dir" = "x" ]; then
  430.     setq statedir "$opt_state_dir" "The State directory"
  431. elif [ ! "x$UCF_STATE_DIR" = "x" ]; then
  432.     setq statedir "$UCF_STATE_DIR" "The State directory"
  433. elif [ ! "x$conf_state_dir" = "x" ]; then
  434.     setq statedir "$conf_state_dir" "The State directory"
  435. else
  436.     setq statedir '/var/lib/ucf'  "The State directory"    
  437. fi
  438.  
  439. # Command line, env variable, config file, or default
  440. if [ ! "x$opt_force_conffold" = "x" ]; then
  441.     setq force_conffold "$opt_force_conffold" "Keep the old file"
  442. elif [ ! "x$UCF_FORCE_CONFFOLD" = "x" ]; then
  443.     setq force_conffold "$UCF_FORCE_CONFFOLD" "Keep the old file"
  444. elif [ ! "x$conf_force_conffold" = "x" ]; then
  445.     setq force_conffold "$conf_force_conffold" "Keep the old file"
  446. else
  447.     force_conffold=''
  448. fi
  449.  
  450. # Command line, env variable, config file, or default
  451. if [ ! "x$opt_force_conffnew" = "x" ]; then
  452.     setq force_conffnew "$opt_force_conffnew" "Replace the old file"
  453. elif [ ! "x$UCF_FORCE_CONFFNEW" = "x" ]; then
  454.     setq force_conffnew "$UCF_FORCE_CONFFNEW" "Replace the old file"
  455. elif [ ! "x$conf_force_conffnew" = "x" ]; then
  456.     setq force_conffnew "$conf_force_conffnew" "Replace the old file"
  457. else
  458.     force_conffnew=''
  459. fi
  460.  
  461. # Command line, env variable, config file, or default
  462. if [ ! "x$opt_force_conffmiss" = "x" ]; then
  463.     setq force_conffmiss "$opt_force_conffmiss" "Replace any missing files"
  464. elif [ ! "x$UCF_FORCE_CONFFMISS" = "x" ]; then
  465.     setq force_conffmiss "$UCF_FORCE_CONFFMISS" "Replace any missing files"
  466. elif [ ! "x$conf_force_conffmiss" = "x" ]; then
  467.     setq force_conffmiss "$conf_force_conffmiss" "Replace any missing files"
  468. else
  469.     force_conffmiss=''
  470. fi
  471.  
  472. if [ -n "$opt_old_mdsum_file" ]; then
  473.     setq old_mdsum_file "$opt_old_mdsum_file" "The md5sum is found here"
  474. elif [ ! "x$UCF_OLD_MDSUM_FILE" = "x" ]; then
  475.     setq old_mdsum_file "$UCF_OLD_MDSUM_FILE" "The md5sum is found here"
  476. elif [ ! "x$conf_old_mdsum_file" = "x" ]; then
  477.     setq old_mdsum_file "$conf_old_mdsum_file" "Replace the old file"
  478. else
  479.     old_mdsum_file="$source_dir/"$(basename "${new_file}")".md5sum";
  480. fi
  481.  
  482.  
  483. ######################################################################
  484. ########                                                     #########
  485. ########               More Sanity checking                  #########
  486. ########                                                     #########
  487. ######################################################################
  488. if [ "X$force_conffold" != "X" -a "X$force_conffnew" != "X" ]; then
  489.     echo >&2 "Error: Only one of force_conffold and force_conffnew should";
  490.     echo >&2 "       be set";
  491.     exit 1;
  492. fi
  493.  
  494. # VERBOSE of 0 is supposed to be the same as not setting VERBOSE
  495. if [ "X$VERBOSE" = "X0" ]; then
  496.     VERBOSE=''
  497. fi
  498.  
  499.  
  500. #
  501. if [ -e "$statedir/hashfile" -a ! -w "$statedir/hashfile" ]; then
  502.     echo >&2 "ucf: do not have write privilege to the state data"
  503.     if [ "X$docmd" = "XYES" ]; then
  504.     exit 1;
  505.     fi
  506. fi
  507.  
  508. if [ ! -d $statedir/cache ]; then
  509.     $action mkdir -p $statedir/cache ;
  510. fi
  511.  
  512. # test and see if this file exists in the database
  513. if [ -e "$statedir/hashfile" ]; then
  514.     if [ "X$VERBOSE" != "X" ]; then
  515.     echo >&2 "The hash file exists"
  516.     echo egrep "[[:space:]]${safe_dest_file}$" "$statedir/hashfile"
  517.     egrep "[[:space:]]${safe_dest_file}$" "$statedir/hashfile" || true
  518.     fi
  519.     lastsum=$(egrep "[[:space:]]${safe_dest_file}$" "$statedir/hashfile" | \
  520.                    awk '{print $1;}' )
  521. fi
  522.  
  523. old_mdsum_dir="$source_dir/"$(basename "${new_file}")".md5sum.d";
  524. cached_file=$(echo $dest_file | tr / :)
  525. ######################################################################
  526. ########                                                     #########
  527. ########                  Debugging dump                     #########
  528. ########                                                     #########
  529. ######################################################################
  530.  
  531. if [ $DEBUG -gt 0 ]; then
  532.     cat <<EOF
  533. The new start file is      \`$new_file\'
  534. The destination is         \`$dest_file\' (\`$safe_dest_file\')
  535. The history is kept under  \'$source_dir\'
  536. The file may be cached at \'$statedir/cache/$cached_file\'
  537. EOF
  538.     if [ -s "$dest_file" ]; then
  539.     echo "The destination file exists, and has md5sum:"
  540.     md5sum "$dest_file"
  541.     else
  542.     echo "The destination file does not exist."
  543.     fi
  544.     if [ "X$lastsum" != "X" ]; then
  545.     echo "The old md5sum exists, and is:"
  546.     echo $lastsum
  547.     else 
  548.     echo "The old md5sum does not exist."
  549.         if [ -d "$old_mdsum_dir" -o -f "$old_mdsum_file" ]; then
  550.             echo "However, there are historical md5sums around."
  551.         fi
  552.     fi
  553.     if [ -e "$new_file" ]; then
  554.     echo "The new file exists, and has md5sum:"
  555.     md5sum "$new_file"
  556.     else 
  557.     echo "The new file does not exist."
  558.     fi
  559.     if [ -d "$old_mdsum_dir" ]; then
  560.     echo "The historical md5sum dir $old_mdsum_dir exists"
  561.     elif [ -f "$old_mdsum_file" ]; then
  562.     echo "The historical md5sum file $old_mdsum_file exists"
  563.     else
  564.     echo "Historical md5sums are not available"
  565.     fi
  566. fi
  567.  
  568. ######################################################################
  569. ########                                                     #########
  570. ########        Short circuit if we are purging              #########
  571. ########                                                     #########
  572. ######################################################################
  573.  
  574. if [ "X$PURGE" = "XYES" ]; then
  575.     if [ "X$VERBOSE" != "X" ]; then
  576.     echo >&2 "Preparing to purge ${dest_file}"
  577.     fi
  578.     purge_md5sum;
  579.     exit 0;
  580. fi
  581.  
  582.  
  583. # now we can restore $@
  584. eval set -- "$saved"
  585.  
  586. ######################################################################
  587. ########                                                     #########
  588. ########                  DebConf stuff                      #########
  589. ########                                                     #########
  590. ######################################################################
  591.  
  592. # Is debconf already running? Kinda tricky, because it will be after the
  593. # confmodule is sourced, so only test before that.
  594. if [ -z "$DEBCONF_ALREADY_RUNNING" ]; then
  595.     if [ "$DEBIAN_HAS_FRONTEND" ]; then
  596.     DEBCONF_ALREADY_RUNNING='YES'
  597.     else
  598.     DEBCONF_ALREADY_RUNNING='NO'
  599.     fi
  600. fi
  601.  
  602. export DEBCONF_ALREADY_RUNNING
  603.  
  604. if [ -z "$DEBCONF_OK" ]; then
  605.     if [ "$DEBCONF_ALREADY_RUNNING" = 'YES' ]; then
  606.     DEBCONF_OK='NO'
  607.     else
  608.     DEBCONF_OK='YES'
  609.     fi
  610. fi
  611.  
  612. # Time to start nagging the users who call ucf without debconf-ok
  613. if [ "$DEBCONF_ALREADY_RUNNING"  = 'YES' ] && [ "$DEBCONF_OK" = NO ]; then
  614.     # Commented out for now, uncomment after a while to begin nagging
  615.     # maintainers to fix their scripts.
  616.     cat \
  617. <<END
  618. *** WARNING: ucf was run from a maintainer script that uses debconf, but
  619.              the script did not pass --debconf-ok to ucf. The maintainer
  620.              script should be fixed to not stop debconf before calling ucf,
  621.              and pass it this parameter. For now, ucf will revert to using
  622.              old-style, non-debconf prompting. Ugh!
  623.  
  624.              Please inform the package maintainer about this problem.
  625. END
  626. fi
  627.  
  628. # Start up debconf or at least get the db_* commands available
  629. if [ -e /usr/share/debconf/confmodule ]; then
  630.     if test $(id -u) = 0; then
  631.     . /usr/share/debconf/confmodule
  632.  
  633.     # Load our templates, just in case our template has
  634.     # not been loaded or the Debconf DB lost or corrupted
  635.     # since then, but only if it is OK to use debconf.
  636.         if [ "$DEBCONF_OK" = 'YES' ]; then
  637.             db_x_loadtemplatefile /var/lib/dpkg/info/ucf.templates ucf
  638.         fi
  639.     else
  640.         echo >&2 "$progname: Not loading confmodule, since we are not running as root."
  641.     fi
  642.     # Only set the title if debconf was not already running.
  643.     # If it was running, then we do not want to clobber the
  644.     # title used for configuring the whole package with debconf.
  645.     if [ "$DEBCONF_ALREADY_RUNNING" = 'NO' ]; then
  646.     if ! db_settitle ucf/title 2>/dev/null; then
  647.               # Older debconf that does not support that command.
  648.             if test $(id -u) = 0; then
  649.         db_title "Modified configuration file"
  650.             else
  651.                 echo >&2 "$progname: Not changing title, since we are not running as root."
  652.             fi
  653.     fi
  654.     fi
  655. fi
  656.     
  657.  
  658.  
  659. ######################################################################
  660. ########                                                     #########
  661. ########                Start Processing                     #########
  662. ########                                                     #########
  663. ######################################################################
  664.  
  665. orig_new_file="$new_file"    # Since sometimes we replace the newfile below
  666. newsum=$(md5sum "$new_file" | awk '{print $1}')
  667.  
  668. # Determine the action for the current file. The default is to ask,
  669. # with non-replacement being the norm.
  670. # If the config dir exists
  671. #   if file in always overwrite, state +=1;
  672. #   fi
  673. #   if file in never overwrite, state +=2;
  674. #   fi
  675. #   if file in ask; state +=4
  676. #   fi
  677. #   if state == 0; then state = default
  678. #   if state >= 4; ask
  679. #   if state == 3;  ask
  680. #   if state == 2; exit
  681. #   if state == 1; then replace_conffile; exit
  682.  
  683. ######################################################################
  684. ########                                                     #########
  685. ########               Do the replacement                    #########
  686. ########                                                     #########
  687. ######################################################################
  688. # Step 1: If we have no record of this file, and dest file
  689. #         does, We need to determine how to initialize the
  690. #         ${old_mdsum_prefix}.old file..               
  691. if [ -e "$dest_file" ]; then
  692.     destsum=$(md5sum "$dest_file"  | awk '{print $1}');
  693.     if [ "X$lastsum" = "X" ]; then
  694. #      a: If we have a directory containing historical md5sums of this
  695. #         file in question, we should look and see if the currently
  696. #         installed file matches any of the old md5sums; in which case
  697. #         it can be silently replaced.
  698.     if [ -d "$old_mdsum_dir" -o -f "$old_mdsum_file" ]; then
  699.         if [ -d "$old_mdsum_dir"  ]; then
  700.         for file in ${old_mdsum_dir}/*; do
  701.             oldsum=$(cat "$file"  | awk '{print $1}');
  702.             if [ "$oldsum" = "$destsum"  ]; then
  703.             if [ "X$force_conffold" = "X" ]; then
  704. #                           Bingo! replace, set the md5sum, and we are done 
  705.                 if [ "X$VERBOSE" != "X" ]; then
  706.                 echo >&2 \
  707.                     "Replacing config file $dest_file with new version"
  708.                 fi
  709.                 replace_conf_file;
  710.                 exit 0;
  711.             else
  712.                 replace_md5sum;
  713.                 cp -pf "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  714.                 exit 0;
  715.             fi
  716.             fi
  717.         done
  718.         elif [ -f "$old_mdsum_file" ]; then
  719.         oldsum=$(egrep "^${destsum}" "$old_mdsum_file" || true)
  720.         if [ "X$oldsum" != "X" ]; then
  721. #                    Bingo
  722.             if [ "X$force_conffold" = "X" ]; then
  723.             if [ "X$VERBOSE" != "X" ]; then
  724.                 echo >&2 \
  725.                 "Replacing config file $dest_file with new version"
  726.             fi
  727.             replace_conf_file;
  728.             exit 0;
  729.             else
  730.             replace_md5sum;
  731.             cp -pf "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  732.             exit 0;
  733.             fi
  734.         fi
  735.         fi
  736. #       Well, nothing matched. We now check to see if the
  737. #       maintainer has an opinion on how to set the ``md5sum of the
  738. #       previously installed version'', since we have no way of
  739. #       determining that automatically. Please note that unless
  740. #       there are limited number of previously released packages
  741. #       (like just one), the maintainer is also making a guess at
  742. #       this point by supplying a historical md5sum default file. 
  743.         if [ "X$VERBOSE" != "X" ]; then
  744.         echo >&2 "Histotical md5sums did not match."
  745.         fi
  746.         if [ -d "$old_mdsum_dir"  ]; then
  747.         if [ -e "${old_mdsum_dir}/default" ]; then
  748.             if [ "X$VERBOSE" != "X" ]; then
  749.             echo >&2 "However, a default entry exists, using it."
  750.             fi
  751.             lastsum=$(cat "${old_mdsum_dir}/default" | \
  752.             awk '{print $1;}')
  753.             do_replace_md5sum=1;
  754.         fi
  755.         elif [ -f "$old_mdsum_file" ]; then
  756.         oldsum=$(egrep "[[:space:]]default$" "$old_mdsum_file" | \
  757.             awk '{print $1;}')
  758.         if [ "X$oldsum" != "X" ]; then
  759. #                    Bingo
  760.             lastsum=$oldsum;
  761.             do_replace_md5sum=1;
  762.         fi
  763.         fi
  764.     fi
  765.  
  766. #       At this point, we are almost certain that either the
  767. #       historical record of md5sums is not complete, or the user has
  768. #       changed the configuration file. Rather than guessing and
  769. #       chosing one of the historical md5sums, we fall through to the
  770. #       solution used if there had been no historical md5sums
  771. #       directory/file.
  772.     if [ "X$lastsum" = "X" ]; then
  773. #      b: We do not have a historical list of md5sums, or none
  774. #         matched, and we still need to initialize the
  775. #         ${old_mdsum_prefix}.old file. We can't determine whther or
  776. #         not they made any changes, so we err on the side of caution
  777. #         and ask'
  778.         if [ "X$VERBOSE" != "X" ]; then
  779.         echo >&2 "No match found, we shall ask."
  780.         fi
  781.         lastsum='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
  782.     fi # the old md5sum file does not exist, and the historical
  783.        # record failed
  784.     fi # the old md5sum file does not exist (bug))
  785. else  # "$dest_file" does not exist
  786. # Step 2: If destfile does not exist, create it, set the file
  787. #         "${old_mdsum_prefix}.old" to the md5sum of the new file, and we
  788. #         are done
  789.     if [ "X$lastsum" = "X" ]; then
  790.         # Ok, so there is no indication that the package was ever
  791.         # installed on this machine.
  792.     echo >&2 ""
  793.     echo >&2 "Creating config file $dest_file with new version"
  794.     replace_conf_file;
  795.     exit 0;
  796.     elif [ "$lastsum" = "$newsum" ]; then
  797.         # OK, new version of the file is the same as the last version
  798.         # we saw. Since the user apparently has deleted the file,
  799.         # nothing needs be done, unless we have been told differently
  800.         if [ "X$force_conffmiss" != "X" ]; then
  801.             echo >&2 ""
  802.         echo >&2 "Recreating deleted config file $dest_file with new version, as asked"
  803.         replace_conf_file;
  804.         exit 0;
  805.         else
  806.             echo >&2 "Not replacing deleted config file $dest_file";
  807.         fi
  808.         
  809.     else
  810.         # OK. New upstream version. 
  811.         if [ "X$force_conffmiss" != "X" ]; then
  812.             # User has said to replace missing files, so we do so, no
  813.             # questions asked.
  814.             echo >&2 ""
  815.         echo >&2 "Recreating deleted config file $dest_file with new version, as asked"
  816.         replace_conf_file;
  817.         exit 0;
  818.         else
  819.             # Even though the user has deleted this file, they should
  820.             # be asked now, unless specified otherwise.
  821.             if [ "X$force_conffold" = "X" ]; then
  822.                 destsum='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
  823.             else
  824.                 exit 0;
  825.         fi    
  826.         fi            
  827.     fi
  828. fi
  829.  
  830. # Here, the destfile exists.
  831.  
  832. # step 3: If the old md5sum and the md5sum of the new file
  833. #         do not match, we need to take action.
  834. if [ "$lastsum" = "$newsum" ]; then
  835.     if [ "X$VERBOSE" != "X" ]; then
  836.     echo >&2 "md5sums match, nothing needs be done."
  837.     fi
  838.     if [ "X$do_replace_md5sum" != "X" ]; then
  839.     replace_md5sum;
  840.     fi
  841.     exit 0;            # Hah. Match. We are done.
  842. fi
  843. #      a: If the md5sum of the dest file is the same as lastsum, replace the 
  844. #         destfile, saying we are replacing old config files
  845. if [ "$destsum" = "$lastsum" ]; then
  846.     if [ "X$force_conffold" = "X" ]; then
  847.     echo >&2 "Replacing config file $dest_file with new version"
  848.     replace_conf_file;
  849.     exit 0;
  850.     else
  851.     replace_md5sum;
  852.     cp -pf "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  853.     exit 0;
  854.     fi
  855. else
  856. #      b: If the md5sum of the dest file differs from lastsum, we need to ask
  857. #         the user what action to take.
  858.     if [ "X$force_conffnew" != "X" ]; then
  859.     echo >&2 "Replacing config file $dest_file with new version"
  860.     echo >&2 "even though the files differ, since you asked for it"
  861.     replace_conf_file;
  862.     exit 0;
  863.     fi
  864.     if [ "X$force_conffold" != "X" ]; then
  865.     replace_md5sum;
  866.     cp -pf "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  867.     exit 0;
  868.     fi
  869. #      c: If the destination file is the same as the new maintianer provided one,
  870. #         we need do nothing.
  871.     if [ "$newsum" = "$destsum" ]; then
  872.     if [ "X$VERBOSE" != "X" ]; then
  873.         echo >&2 "md5sums of the file in place matches, nothing needs be done."
  874.     fi
  875.     replace_md5sum;
  876.     exit 0;            # Hah. Match. We are done.
  877.     fi
  878.  
  879.  
  880.     done='NO';
  881.     while [ "X$done" = "XNO" ]; do
  882.     if [ "$DEBCONF_OK" = "YES" ] && [ "$DEBIAN_HAS_FRONTEND" ]; then
  883.         # Use debconf to prompt.
  884.         if [ -e "$statedir/cache/$cached_file" ]; then
  885.             templ=ucf/changeprompt_threeway
  886.         else
  887.             templ=ucf/changeprompt
  888.         fi
  889.         if [ "X$override_template" != "X" ]; then
  890.             choices="$(db_metaget $templ Choices-C)"
  891.             choices2="$(db_metaget $override_template Choices-C)"
  892.             if [ "$choices" = "$choices2" ]; then
  893.                 templ=$override_template
  894.             fi
  895.         fi
  896.         db_fset $templ seen false
  897.         db_reset $templ
  898.         db_subst $templ FILE "$dest_file"
  899.         db_subst $templ BASENAME $(basename "$dest_file")
  900.         db_input critical $templ || true
  901.         if ! db_go; then
  902.             # The current ucf interface does not provide a way for it
  903.             # to tell its caller that the user chose to back up.
  904.             # However, we could get here, if the caller turned on
  905.             # debconf's backup capb. The best thing to do seems to be
  906.             # to ignore requests to back up.
  907.             continue
  908.         fi
  909.         db_get $templ
  910.         ANSWER="$RET"
  911.     else
  912.             echo >&2 "Need debconf to interact"
  913.             exit 2
  914. ########################################################################################
  915. #         # Prompt without using debconf.                                        #
  916. #         cat >&2 <<EOPRMT                                                       #
  917. # Configuration file \`$dest_file'                                                     #
  918. #  ==> File on system created by you or by a script.                                   #
  919. #  ==> File also in package provided by package maintainer.                            #
  920. #    What would you like to do about it ?  Your options are:                           #
  921. #     Y or I  : install the package maintainer's version                               #
  922. #     N or O  : keep your currently-installed version                                  #
  923. #       D     : show the differences between the versions                              #
  924. #       S     : show the side-by-side differences between the versions                 #
  925. # EOPRMT                                                                               #
  926. #         if [ "X$THREEWAY" != "X" -a -e "$statedir/cache/$cached_file" ]; then  #
  927. #             cat >&2 <<EOTD                                                 #
  928. #     3 or T  : show a three way difference between current, older,                    #
  929. #               and new versions of the file                                           #
  930. #       M     : Do a 3 way merge between current, older,                               #
  931. #               and new versions of the file [Very Experimental]                       #
  932. # EOTD                                                                                 #
  933. #         fi                                                                     #
  934. #         cat >&2 <<EOPEND                                                       #
  935. #       Z     : start a new shell to examine the situation                             #
  936. #  The default action is to keep your current version.                                 #
  937. # EOPEND                                                                               #
  938. #         if [ "X$THREEWAY" != "X" -a -e "$statedir/cache/$cached_file" ]; then  #
  939. #             echo -n >&2 "*** " $(basename "$dest_file") \                  #
  940. #                 " (Y/I/N/O/D/3/T/M/Z) [default=N] ?"                       #
  941. #         else                                                                   #
  942. #             echo -n >&2 "*** " $(basename "$dest_file") \                  #
  943. #                 " (Y/I/N/O/D/Z) [default=N] ?"                             #
  944. #         fi                                                                     #
  945. #           read -e ANSWER </dev/tty                                               #
  946. ########################################################################################
  947.     fi
  948.  
  949.     case "$ANSWER" in
  950.         install_new|y|Y|I|i)
  951.         echo >&2 "Replacing config file $dest_file with new version"
  952.         RETAIN_OLD=YES
  953.         replace_conf_file;
  954.         exit 0;
  955.         ;;
  956.         diff|D|d)
  957.         if [ -e "$dest_file" ]; then
  958.             DIFF="$(diff -uBbw "$dest_file" "$new_file")" || true
  959.         else
  960.             DIFF="$(diff -uBbw /dev/null "$new_file")" || true
  961.         fi
  962.         show_diff "$DIFF"
  963.         ;;
  964.         sdiff|S|s)
  965.         if [ -e "$dest_file" ]; then
  966.             DIFF="$( sdiff -BbW "$dest_file" "$new_file")"  || true
  967.         else
  968.             DIFF="$(sdiff -BbW /dev/null "$new_file")"  || true
  969.         fi
  970.         show_diff "$DIFF"
  971.         ;;
  972.         diff_threeway|3|t|T)
  973.         if [ -e "$statedir/cache/$cached_file" \
  974.             -a "X$THREEWAY" != "X" ]; then
  975.                     if [ -e "$dest_file" ]; then
  976.                 DIFF="$(diff3 -L Current -L Older -L New -A \
  977.                 "$dest_file" "$statedir/cache/$cached_file" \
  978.                 "$new_file")"  || true
  979.                     else
  980.                         DIFF="$(diff3 -L Current -L Older -L New -A \
  981.                 /dev/null "$statedir/cache/$cached_file" \
  982.                 "$new_file")"  || true
  983.                     fi
  984.             show_diff "$DIFF"
  985.         else 
  986.             if [ -e "$dest_file" ]; then
  987.             DIFF="$(diff -uBbw "$dest_file" "$new_file")"  || true
  988.             else
  989.             DIFF="$(diff -uBbw /dev/null "$new_file")"  || true
  990.             fi
  991.             show_diff "$DIFF"
  992.         fi
  993.         ;;
  994.         merge_threeway|M|m)
  995.         echo >&2 "Merging changes into the new version"
  996.         if [ -e "$statedir/cache/$cached_file" \
  997.             -a "X$THREEWAY" != "X" ]; then
  998.             ret=0
  999.             diff3 -L Current -L Older -L New -m \
  1000.             "$dest_file" "$statedir/cache/$cached_file" \
  1001.             "$new_file" > $dest_file.${NEW_SUFFIX} || ret=$?
  1002.                     case "$ret" in
  1003.                         0)
  1004.                     new_file="$dest_file.${NEW_SUFFIX}"
  1005.                     RETAIN_OLD=YES
  1006.                     replace_conf_file
  1007.                 rm -f "$dest_file.${NEW_SUFFIX}" # don't need this around no mo'
  1008.                 exit 0
  1009.                             ;;
  1010.                         *)
  1011.                 db_subst ucf/conflicts_found dest_file "$dest_file"
  1012.                 db_subst ucf/conflicts_found NEW_SUFFIX "${NEW_SUFFIX}"
  1013.                 db_input critical ucf/conflicts_found || true
  1014.                 db_go || true
  1015.                 ;;
  1016.                     esac
  1017.         else 
  1018.             replace_conf_file
  1019.             rm -f "$dest_file.${NEW_SUFFIX}" # don't need this around no mo'
  1020.             exit 0
  1021.         fi
  1022.         ;;
  1023.         shell|Z|z)
  1024.                 if tty -s; then
  1025.             bash >/dev/tty </dev/tty || true
  1026.                 elif [ -n "$DISPLAY" ]; then
  1027.                     x-terminal-emulator || true
  1028.                 else
  1029.                     # Don't know what to do
  1030.                     echo >&2 "No terminal, and no DISPLAY set, can't fork shell."
  1031.                 fi
  1032.         ;;
  1033.         keep_current|n|N|o|O|'')
  1034.         replace_md5sum;
  1035.  
  1036.         cp -pf "$orig_new_file" "$dest_file.${DIST_SUFFIX}"
  1037.         exit 0;
  1038.         ;;
  1039.         *)
  1040.         if [ "$DEBCONF_OK" = "YES" ]; then
  1041.             echo "Error: unknown response from debconf:'$RET'" >&2
  1042.             exit 1
  1043.         else
  1044.             echo
  1045.             echo "Please answer with one of the single letters listed." >&2
  1046.             echo
  1047.         fi
  1048.     esac
  1049.     done
  1050. fi
  1051.  
  1052. db_stop
  1053.  
  1054. exit 0;
  1055.